﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.ComponentModel;
using Ferpect.ComponentModel.Design;
using Microsoft.Xna.Framework;

namespace Ferpect.ComponentModel.Sprites
{
    [Description("Draws a line of text.")]
    public class TextSprite : Sprite
    {
        private AmbientProperties ambientProperties;
        private SpriteFont font;
        private string fontName;
        private string text;

        public TextSprite()
        {
            this.text = String.Empty;
            this.fontName = String.Empty;
        }

        [Category("Appearance")]
        [DefaultValue("")]
        [DisplayName("Font")]
        [TypeConverter(TypeConverters.SpriteFontConverter)]
        [RefreshContent()]
        public string FontName
        {
            get { return this.fontName; }
            set { this.fontName = value; }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public SpriteFont Font
        {
            get
            {
                if (this.font == null && this.ambientProperties != null)
                {
                    return this.ambientProperties.Font;
                }
                return this.font;
            }
            set
            {
                this.font = value;
            }
        }

        [DefaultValue("")]
        [Category("Appearance")]
        [Description("The text associated with the sprite.")]
        public string Text
        {
            get { return this.text; }
            set
            {
                this.text = value;
                this.ResetRelativeOrigin();
            }
        }

        public override void LoadContent(ContentManager contentManager)
        {
            ISite site = this.Site;
            if (site != null)
            {
                this.ambientProperties = (AmbientProperties)site.GetService(typeof(AmbientProperties));
            }
            if (!String.IsNullOrEmpty(this.fontName) && contentManager != null)
            {
                this.font = contentManager.Load<SpriteFont>(this.fontName);
            }
            this.ResetRelativeOrigin();
        }

        public override void UnloadContent()
        {
            this.font = null;
        }

        public override void Draw(SpriteBatchStack spriteBatchStack)
        {
            if (spriteBatchStack == null) throw new ArgumentNullException("spriteBatchStack");

            if (this.Visible)
            {
                spriteBatchStack.Current.DrawString(this.Font, this.Text, this.Position, this.Color, this.Rotation, this.Origin, this.Scale, this.Effects, this.LayerDepth);
            }
        }

        public override Vector2 GetSize()
        {
            if (this.Font != null && !String.IsNullOrEmpty(this.Text))
            {
                return this.Font.MeasureString(this.Text);
            }
            return Vector2.Zero;
        }
    }
}
